# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json

import pymongo
from itemadapter.adapter import ItemAdapter
from scrapy.exceptions import DropItem


class CleanItemPipeline:
    def process_item(self, item, spider):
        adapter = ItemAdapter(item)
        if adapter.get('name') is None or adapter.get('intro') is None:
            raise DropItem(f"Missing name or intro in {item}")
        return item


class JsonPipeline():
    def open_spider(self, spider):
        self.file = open(f'{spider.name}.jsonl', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        line = ''.join([json.dumps(
            ItemAdapter(item).asdict(),
            ensure_ascii=False
        ), "\n"])
        self.file.write(line)
        return item

    def close_spider(self, spider):
        self.file.close()


class MongoPipeline:
    collection_name = 'poetry'

    def __init__(self, mongo_uri, mongo_db):
        self.mongo_uri = mongo_uri
        self.mongo_db = mongo_db

    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            mongo_uri=crawler.settings.get('MONGO_URI'),
            mongo_db=crawler.settings.get('MONGO_DB', 'items')
        )

    def open_spider(self, spider):
        self.client = pymongo.MongoClient(self.mongo_uri)
        self.db = self.client.get_database(self.mongo_db)
        self.collection = self.db.get_collection(self.collection_name)

    def process_item(self, item, spider):
        self.collection.insert_one(ItemAdapter(item).asdict())
        return item

    def close_spider(self, spider):
        self.client.close()
